<template>
  <!-- 筛选搜索 -->
  <div class="search-box">
    <div class="search1">
      <div class="left">
        <el-icon>
          <Search />
        </el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="resetFn">重置</el-button>
        <el-button type="primary" @click="ListFn">查询搜索</el-button>
      </div>
    </div>

    <div class="search">
      <div class="search_input">
        <span>商品名称：</span>
        <el-input placeholder="商品名称" v-model="form.productName"></el-input>
      </div>
      <div class="search_input">
        <span>推荐状态：</span>
        <el-select v-model="form.recommendStatus" class="m-2" placeholder="全部" size="large">
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
    </div>
  </div>
  <!-- 数据列表 -->
  <div class="search-box">
    <div class="search2">
      <div class="left">
        <el-icon>
          <Tickets />
        </el-icon>
        <span>数据列表</span>
      </div>
      <div class="right">
        <el-button @click="showGood = true">选择商品</el-button>
      </div>
    </div>
  </div>
  <!-- 表格 -->
  <el-table :data="list" style="width: 100%" border @selection-change="handleSelectionChange">
    <el-table-column type="selection" />

    <el-table-column prop="id" label="编号" />
    <el-table-column prop="productName" label="商品名称" />
    <el-table-column label="是否推荐">
      <template #default="{ row }">
        <el-switch v-model="row.recommendStatus" size="large" :active-value="1" :inactive-value="0"
          @click="switchStatus(row)" />
      </template>
    </el-table-column>
    <el-table-column prop="sort" label="排序" />

    <el-table-column label="状态">
      <template #default="{ row }">
        <span>{{ row.recommendStatus == 1 ? "推荐中" : "未推荐" }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="address" label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="editSort(row)">设置排序</el-button>
        <el-button text type="primary" @click="deleteFn(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="page">
    <div class="btn">
      <el-select v-model="value2" class="m-2" placeholder="批量操作" size="large">
        <el-option v-for="item in statusList2" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-button type="primary" @click="ConfirmFn">确定</el-button>
    </div>
    <ElConfigProvider :locale="locale">
      <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle" />
    </ElConfigProvider>
  </div>
  <showGoods :show="showGood" @hide="closeFn" />
  <editSortVue :show="showDialog" @hide="closeDialog" :id="form.id" :sort="form.sort" />
</template>
<script setup lang="ts">
import { ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import {
  getNewProductAPI,
  switchNewProductStatusAPI,
  deleteNewProductAPI,
} from "./api";
import useTable from "@/hooks/useTable";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import {
  ElButton,
  ElConfigProvider,
  ElIcon,
  ElInput,
  ElMessage,
  ElMessageBox,
  ElOption,
  ElPagination,
  ElSelect,
  ElSwitch,
  ElTable,
  ElTableColumn,
} from "element-plus";
import showGoods from "./components/showGoods.vue";
import editSortVue from "./components/editSort.vue";

let showGood = ref<boolean>(false);
const closeFn = () => {
  showGood.value = !showGood;
};

const value2 = ref();
const statusList2 = ref([
  {
    value: 1,
    label: "设为推荐",
  },
  {
    value: 0,
    label: "取消推荐",
  },
  {
    value: 2,
    label: "删除",
  },
]);

const statusList = ref([
  {
    value: 1,
    label: "推荐中",
  },
  {
    value: 0,
    label: "未推荐",
  },
]);

// 控制添加编辑角色框的显示与隐藏
const showDialog = ref(false);
// 关闭设置排序弹出框的再次请求数据
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) ListFn();
};
// 点击排序传入改id
const editSort = (row: any) => {
  form.value.id = row.id;
  form.value.sort = row.sort;
  // 把editSort 显示出来
  showDialog.value = true;
};

// 确定批量操作
const ConfirmFn = () => {
  if (value2.value == 2) {
    ElMessageBox.confirm("是否要删除该推荐??", "Warning", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        deleteNewProductAPI(Ids.value);
        ListFn();
        ElMessage.success("删除成功");
      })
      .catch(() => {
        ElMessage.success("已取消操作");
      });
  } else if (value2.value == 0) {
    batchSwitch();
  } else if (value2.value === 1) {
    batchSwitch();
  }
};

// 封装批量修改操作api
const batchSwitch = () => {
  ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      switchNewProductStatusAPI(Ids.value, value2.value)
        .then((res) => {
          if (res.code === 200) {
            ElMessage.success("状态修改成功");
            ListFn();
          } else {
            value2.value = value2.value === 1 ? 0 : 1;
          }
        })
        .catch(() => {
          value2.value = value2.value === 1 ? 0 : 1;
          ElMessage.error("状态修改失败");
        });
    })
    .catch(() => {
      ElMessage.success("已取消操作");
    });
};

// 删除推荐
const deleteFn = (Ids: number[]) => {
  ElMessageBox.confirm("是否要删除该推荐??", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteNewProductAPI(Ids);
      ListFn();
      ElMessage.success("删除成功");
    })
    .catch(() => {
      ElMessage.success("已取消操作");
    });
};

const Ids = ref<number[]>([]);

// 勾选时获取ids
const handleSelectionChange = (val: any[]) => {
  // console.log(val.map(item => item.id));
  Ids.value = val.map((item) => item.id);
};

// 传入数据
const form = ref({
  // 搜索框数据
  productName: "",
  recommendStatus: "",
  id: 0,
  sort: 0,
});

// 是否推荐品牌
const switchStatus = (row: IBrandList) => {
  Ids.value = [row.id];
  changeSwitchStatus(row);
};

// 修改状态开关
const changeSwitchStatus = (row: IBrandList) => {
  ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      switchNewProductStatusAPI(Ids.value, row.recommendStatus)
        .then((res) => {
          if (res.code === 200) {
            ElMessage.success("状态修改成功");
          } else {
            row.recommendStatus = row.recommendStatus === 1 ? 0 : 1;
          }
        })
        .catch(() => {
          row.recommendStatus = row.recommendStatus === 1 ? 0 : 1;
        });
    })
    .catch(() => {
      ElMessage.success("已取消操作");
      row.recommendStatus = row.recommendStatus === 1 ? 0 : 1;
    });
};
// 重置
const resetFn = () => {
  form.value.productName = "";
  ListFn();
};

const locale = zhCn;
let {
  list,
  total,
  pageNum,
  pageSize,
  pageChangeHandle,
  pageSizeChangeHandle,
  ListFn,
} = useTable(getNewProductAPI, form.value);
</script>
<style lang="less" scoped>
.search-box {
  box-shadow: 0 0 10px 0px #ccc;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;

  .search1 {
    padding: 20px 0;
    display: flex;
    justify-content: space-between;
  }

  .search {
    padding: 20px 0;
    display: flex;

    .search_input {
      display: flex;
      margin-right: 20px;
      align-items: center;

      &:first-child {
        width: 300px;
        margin-left: 40px;

        span {
          width: 125px;
          margin-right: 12px;
        }
      }
    }
  }

  .search2 {
    display: flex;
    width: 100%;
    justify-content: space-between;

    .left {
      display: flex;
      align-items: center;

      span {
        line-height: 20px;
        font-size: 14px;
        align-items: center;
      }
    }
  }
}

:deep(.el-table__cell) {
  text-align: center !important;
  height: 57px;
}

:deep(.cell) {
  text-align: center !important;
}

:deep(.el-input__inner) {
  height: 30px;
}

.page {
  margin: 20px 0 10px 0;
  display: flex;
  justify-content: space-between;

  .btn {
    :deep(.el-input__wrapper) {
      margin-right: 20px;
    }
  }
}
</style>
